/*
 * @(#)2020年5月27日 V1.0
 * 
 * Copyright 2020 WELL-SOFT, Inc. All rights reserved.
 */
package com.wellsoft.pt.app.dingtalk.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wellsoft.pt.app.dingtalk.entity.MultiOrgDingUserInfo;
import com.wellsoft.pt.app.dingtalk.service.MultiOrgDingUserInfoService;
import com.wellsoft.pt.app.dingtalk.service.WorkRecordService;
import com.wellsoft.pt.app.dingtalk.utils.DingtalkApiUtils;
import com.wellsoft.pt.jpa.event.WellptEventListener;
import com.wellsoft.pt.multi.org.facade.service.MultiOrgUserService;
import com.wellsoft.pt.workflow.event.WorkDoneEvent;

/**
 * Description: 如何描述该类
 *  
 * @author zhongzh
 * @date 2020年5月27日
 * @version 1.0
 * 
 * <pre>
 * 修改记录:
 * 修改后版本	修改人		修改日期			修改内容
 * 2020年5月27日.1	zhongzh		2020年5月27日		Create
 * </pre>
 *
 */
@Component
public class WorkRecordUpdateListener extends WellptEventListener<WorkDoneEvent> {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private WorkRecordService workRecordService;

	@Autowired
	private MultiOrgUserService multiOrgUserService;

	@Autowired
	private MultiOrgDingUserInfoService multiOrgDingUserInfoService;

	/** 
	 * 
	 */
	@Override
	public void onApplicationEvent(WorkDoneEvent event) {
		String flowInstUuid = event.getFlowInstUuid();
		String taskInstUuid = event.getTaskInstUuid();
		MultiOrgDingUserInfo multiOrgDingUserInfo = multiOrgDingUserInfoService.getByUserId(event.getUserId());
		if (multiOrgDingUserInfo == null) {
			logger.warn("flowInstUuid[" + flowInstUuid + "]taskInstUuid[" + taskInstUuid + "]userId["
					+ event.getUserId() + "]未找到钉钉用户");
			return;
		}
		String dingUserId = multiOrgDingUserInfo.getDing_userId();
		DingtalkApiUtils.updateWorkRecord(taskInstUuid, dingUserId, DingtalkApiUtils.getAccessToken());
	}

}
